package com.bit.dreamer;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class Dreamlist_haveActivity extends Activity {
	ArrayList<ListContent> list;
	DreamAdapter adapter;
	ListView listView;
	
	DreamHelper helper;
	SQLiteDatabase db;
	Cursor rs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dreamlistview);
		
		helper = new DreamHelper(this);
		
		list= new ArrayList<ListContent>();
		adapter = new DreamAdapter(this, R.layout.list_design, list);
		
		listView = (ListView) findViewById(R.id.list);
		listView.setAdapter(adapter);
		db = helper.getReadableDatabase();
		
		rs = db.rawQuery("SELECT _id, title, dreamdate, img FROM D_have_tb", null);
		while (rs.moveToNext()) {
			Log.d("_id", rs.getColumnIndex("_id") + "");
//			byte[] b = rs.getBlob(3);
//			Bitmap bit = BitmapFactory.decodeByteArray(b , 0 , b.length);
			
			Bitmap bitmap;
			Bitmap resized = null;
			try {
				bitmap = Images.Media.getBitmap(getContentResolver(), Uri.parse(rs.getString(3)));
				int height = bitmap.getHeight();
				int width = bitmap.getWidth();
				if(height > 2048) {
					while (height > 2048) {
						resized = Bitmap.createScaledBitmap(bitmap, (width * 2048) / height, 2048, true);
						height = resized.getHeight();
						width = resized.getWidth();
						list.add(new ListContent(rs.getString(1), resized, rs.getString(2), rs.getInt(0)));
					}
				} else {
					list.add(new ListContent(rs.getString(1), bitmap, rs.getString(2), rs.getInt(0)));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}//while

		adapter.notifyDataSetChanged();
		System.gc();
		rs.close();
		helper.close();
		
		Button btn=(Button)findViewById(R.id.add);
		btn.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View V) {
				Intent intent= new Intent(getApplicationContext(), DreaminsertActivity.class);
				intent.putExtra("sel", 2);
				startActivity(intent);
				
			}
		
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(Dreamlist_haveActivity.this, DreameditActivity.class);
				intent.putExtra("_id", list.get(position)._id);
				intent.putExtra("sel", 2);
				startActivity(intent);
			}
		});
		
		listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
				
				AlertDialog.Builder db_delete = new AlertDialog.Builder(Dreamlist_haveActivity.this).setMessage("정말 삭제하시겠습니까?");
				db_delete.setPositiveButton("예", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						SQLiteDatabase db = helper.getWritableDatabase();
						String query = "delete from D_have_tb where title='" + list.get(position).title + "';";
						db.execSQL(query);
						list.remove(position);
						adapter.notifyDataSetChanged();
						helper.close();
						dialog.cancel();
					}
				});

				db_delete.setNegativeButton("아니요", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
				db_delete.show();
				return false;
			}
		});
	}


}
